////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                        Generated Initialization File                                           //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


#include "C8051F380_defs.h"
#include "init_device.h"
#include "debug.h"
#include "hw_uart.h"


#define SYSCLK      48000000           // SYSCLK frequency in Hz
#define BAUDRATE       38400           // Baud rate of UART in bps


// Peripheral specific initialization functions,
// Called from the Init_Device() function
void PCA_Init()
{
    PCA0MD    &= ~0x40;
    PCA0MD    = 0x00;
}
void SPI_Init()
{
    SPI0CFG   = 0x40;
    SPI0CN    = 0x0D;  //NSS pull high  /*****/
    SPI0CKR   = 0x02; /*8m*/
    //SPI0CKR   = 0x0b; /*2m*/
}
void UART_Init()
{
    SCON0     = 0x10;
    if (SYSCLK/BAUDRATE/2/256 < 1) 
    {
        TH1 = -(SYSCLK/BAUDRATE/2);
        CKCON &= ~0x0B;                  // T1M = 1; SCA1:0 = xx
        CKCON |=  0x08;
    }
    else if (SYSCLK/BAUDRATE/2/256 < 4) 
    {
        TH1 = -(SYSCLK/BAUDRATE/2/4);
        CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01                  
        CKCON |=  0x01;
    } 
    else if (SYSCLK/BAUDRATE/2/256 < 12)
    {
        TH1 = -(SYSCLK/BAUDRATE/2/12);
        CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
    }
    else 
    {
        TH1 = -(SYSCLK/BAUDRATE/2/48);
        CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10
        CKCON |=  0x02;
    }
    TL1 = TH1;                          // Init Timer1
    TMOD &= ~0xf0;                      // TMOD: timer 1 in 8-bit autoreload
    TMOD |=  0x20;                       
    TR1 = 1;                            // START Timer1
    TI0 = 1;                            // Indicate TX0 ready
}
void Port_IO_Init()
{
    // P0.0  -  Skipped,     Push-Pull,  Digital
    // P0.1  -  SCK  (SPI0), Push-Pull,  Digital
    // P0.2  -  MISO (SPI0), Open-Drain, Digital
    // P0.3  -  MOSI (SPI0), Push-Pull,  Digital
    // P0.4  -  TX0 (UART0), Push-Pull,  Digital
    // P0.5  -  RX0 (UART0), Open-Drain, Digital
    // P0.6  -  Skipped,     Open-Drain, Digital
    // P0.7  -  NSS  (SPI0), Push-Pull,  Digital

    // P1.0  -  SYSCLK,      Push-Pull,  Digital
    // P1.1  -  Unassigned,  Open-Drain, Digital
    // P1.2  -  Unassigned,  Open-Drain, Digital
    // P1.3  -  Unassigned,  Open-Drain, Digital
    // P1.4  -  Unassigned,  Open-Drain, Digital
    // P1.5  -  Unassigned,  Open-Drain, Digital
    // P1.6  -  Unassigned,  Open-Drain, Digital
    // P1.7  -  Unassigned,  Open-Drain, Digital

    // P2.0  -  Unassigned,  Open-Drain, Digital
    // P2.1  -  Unassigned,  Open-Drain, Digital
    // P2.2  -  Skipped,     Push-Pull,  Digital
    // P2.3  -  Skipped,     Push-Pull,  Digital
    // P2.4  -  Unassigned,  Open-Drain, Digital
    // P2.5  -  Unassigned,  Open-Drain, Digital
    // P2.6  -  Unassigned,  Open-Drain, Digital
    // P2.7  -  Unassigned,  Open-Drain, Digital

    // P3.0  -  Unassigned,  Open-Drain, Digital
    // P3.1  -  Unassigned,  Open-Drain, Digital
    // P3.2  -  Unassigned,  Open-Drain, Digital
    // P3.3  -  Unassigned,  Open-Drain, Digital
    // P3.4  -  Unassigned,  Open-Drain, Digital
    // P3.5  -  Unassigned,  Open-Drain, Digital
    // P3.6  -  Skipped,     Push-Pull,  Digital
    // P3.7  -  Skipped,     Push-Pull,  Digital

    P0MDOUT   = 0x9B;
    P1MDOUT   = 0x01;
    //P2MDOUT   = 0x0C;
    //P2MDOUT   = 0x0D;
    P2MDOUT   = 0x3F;
    P3MDOUT   = 0xC0;
    P4MDOUT   = 0x0C;
    P0SKIP    = 0x41;
    P2SKIP    = 0x0C;
    P3SKIP    = 0xC0;
    XBR0      = 0x0B; /*SPI,SYSCLK,UART TX0, RX0 routed to Port pin*/
    XBR1      = 0xC0; /*Crossbar enabled,Weak Pullups disabled*/
}


void Oscillator_Init()
{
    OSCICN    = 0xC3;
}

void Interrupts_Init()
{
    IP        = 0x40;  //SPI
    EIE1      = 0x02;  //EUSB0
    IT01CF    = 0x06;
    IE        = 0xC0;    //EA ESPI0  
}

// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
    PCA_Init(); 
    #ifdef UART_DEBUG    
        UART_Init();
    #endif
    SPI_Init();
    Port_IO_Init(); 
    Oscillator_Init();
    Interrupts_Init();
	Uart0_Init();
}

/*-------------------------------End Of File------------------------------*/

